package com.youkeyi.ddy.cloud.application.business.crm.supplier.controller;

import com.youkeyi.ddy.cloud.application.business.crm.support.service.UserSupportService;
import com.youkeyi.ddy.cloud.common.api.application.business.crm.supplier.dto.BusinessSupplierListCloudDto;
import com.youkeyi.ddy.cloud.common.api.application.business.crm.supplier.dto.BusinessSupplierListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.application.business.crm.supplier.param.BusinessSupplierCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.business.crm.supplier.param.BusinessSupplierDeleteCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.business.crm.supplier.param.BusinessSupplierListCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.business.crm.supplier.param.BusinessSupplierUpdateCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.business.crm.supplier.BusinessSupplierCloudService;
import com.youkeyi.ddy.cloud.common.api.application.system.user.dto.SystemUserListByOrgCloudDto;
import com.youkeyi.ddy.cloud.common.api.application.system.user.dto.SystemUserListByOrgItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.application.system.user.param.SystemUserListByOrgCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.supplier.CrmSupplierCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.crm.supplier.dto.CrmSupplierListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.crm.supplier.param.CrmSupplierCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.supplier.param.CrmSupplierDeleteCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.supplier.param.CrmSupplierQueryCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.supplier.param.CrmSupplierUpdateCloudParam;
import com.youkeyi.ddy.cloud.common.context.YoukeyiSystemContext;
import com.youkeyi.ddy.cloud.common.enums.ResponseCodeEnum;
import com.youkeyi.ddy.cloud.common.response.ResponseContent;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * BusinessSupplierController
 *
 * @Author liuxiawang
 * @Date 2024/5/11 5:58 PM
 * @Location shenzhen.china
 */
@Service
public class BusinessSupplierController implements BusinessSupplierCloudService {

    @Autowired
    private CrmSupplierCloudService crmSupplierCloudService;
    @Autowired
    private UserSupportService userSupportService;

    @Override
    public BusinessSupplierListCloudDto list(BusinessSupplierListCloudParam param) {
        BusinessSupplierListCloudDto dto = new BusinessSupplierListCloudDto();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        String positionCode = YoukeyiSystemContext.getPositionCode();
        String organizationKey = YoukeyiSystemContext.getOrganizationKey();
        if (StringUtils.isBlank(corpKey)) {
            dto.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return dto;
        }
        if (null == param) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }
        CrmSupplierQueryCloudParam supplierQueryCloudParam = new CrmSupplierQueryCloudParam();



        // 判断当前登陆人是主管、副主管还是普通成员
        if (StringUtils.isBlank(positionCode)) {
            dto.setErrorMessage(ResponseCodeEnum.SYSTEM_UNKNOWN_POSITION_CODE_ERROR.getMessage());
            return dto;
        }
        // 主管副主管
        if (positionCode.endsWith("-ZG") || positionCode.endsWith("-FZG")) {
            // 查询他的组织下属人员的
            List<String> userKeys = new ArrayList<>();
            if (StringUtils.isNotBlank(organizationKey)) {
                SystemUserListByOrgCloudParam userListByOrgCloudParam = new SystemUserListByOrgCloudParam();
                userListByOrgCloudParam.setOrganizationPrimaryKeys(Collections.singletonList(organizationKey));
                SystemUserListByOrgCloudDto userDtos = userSupportService.listUserByOrg(userListByOrgCloudParam);
                if (null != userDtos && CollectionUtils.isNotEmpty(userDtos.getRows())) {
                    userKeys.addAll(userDtos.getRows().stream().map(SystemUserListByOrgItemCloudDto::getUserPrimaryKey).collect(Collectors.toList()));
                }
            }
            if (CollectionUtils.isEmpty(userKeys)) {
                dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
                return dto;
            }

            // 查询出来的下属用户key
            supplierQueryCloudParam.setCorpKeys(Collections.singletonList(corpKey));
            supplierQueryCloudParam.setSourceUserKeys(userKeys);
        }
        // 普通成员
        else if (positionCode.endsWith("-CY")) {
            supplierQueryCloudParam.setCorpKeys(Collections.singletonList(corpKey));
            supplierQueryCloudParam.setSourceUserKeys(Collections.singletonList(YoukeyiSystemContext.getUserPrimaryKey()));
        }
        // 超管
        else if (positionCode.equals("superadmin")) {
            // 超级管理员不需要增加什么过滤
        }
        // 管理员
        else if (positionCode.equals("-admin")) {
            // 管理员看主体的
            supplierQueryCloudParam.setCorpKeys(Collections.singletonList(corpKey));
        }
        // 未知的岗位
        else {
            dto.setErrorMessage(ResponseCodeEnum.SYSTEM_UNKNOWN_POSITION_CODE_ERROR.getMessage());
            return dto;
        }

        supplierQueryCloudParam.setSupplierPrimaryKeys(param.getSupplierPrimaryKeys());
        supplierQueryCloudParam.setSourceOrganizationKeys(param.getSourceOrganizationKeys());
        supplierQueryCloudParam.setCompanyNameFuzzy(param.getCompanyNameFuzzy());
        supplierQueryCloudParam.setPageSize(param.getPageSize());
        supplierQueryCloudParam.setCurrentPage(param.getCurrentPage());
        CrmSupplierListCloudDto supplierDtos = crmSupplierCloudService.query(supplierQueryCloudParam);
        if (null != supplierDtos && CollectionUtils.isNotEmpty(supplierDtos.getRows())) {
            List<BusinessSupplierListItemCloudDto> items = new ArrayList<>();
            supplierDtos.getRows().forEach(supplier -> {
                BusinessSupplierListItemCloudDto item = new BusinessSupplierListItemCloudDto();
                item.setSupplierPrimaryKey(supplier.getSupplierPrimaryKey());
                item.setCorpKey(supplier.getCorpKey());
                item.setSourceOrganizationKey(supplier.getSourceOrganizationKey());
                item.setSourceOrganizationName(supplier.getSourceOrganizationName());
                item.setSourceUserKey(supplier.getSourceUserKey());
                item.setSourceUserName(supplier.getSourceUserName());
                item.setCategory(supplier.getCategory());
                item.setCategoryName(supplier.getCategoryName());
                item.setCompanyName(supplier.getCompanyName());
                item.setName(supplier.getName());
                item.setPhone(supplier.getPhone());
                item.setWechatId(supplier.getWechatId());
                item.setPlate(supplier.getPlate());
                item.setPlateKey(supplier.getPlateKey());
                item.setCollaborativeProjectsKey(supplier.getCollaborativeProjectsKey());
                item.setCollaborativeProjects(supplier.getCollaborativeProjects());
                item.setCooperationModeDesc(supplier.getCooperationModeDesc());
                item.setCommissionSharingDesc(supplier.getCommissionSharingDesc());
                item.setRemark(supplier.getRemark());
                item.setAnnexKey(supplier.getAnnexKey());
                item.setIsDeleted(supplier.getIsDeleted());
                item.setIsDeletedName(supplier.getIsDeletedName());
                item.setCreateUserKey(supplier.getCreateUserKey());
                item.setCreateUserName(supplier.getCreateUserName());
                item.setCreateTime(supplier.getCreateTime());
                item.setUpdateUserKey(supplier.getUpdateUserKey());
                item.setUpdateUserName(supplier.getUpdateUserName());
                item.setUpdateTime(supplier.getUpdateTime());
                items.add(item);
            });
            dto.setRows(items);
            dto.setTotal(supplierDtos.getTotal());
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }

    @Override
    public ResponseContent create(BusinessSupplierCreateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param
                || StringUtils.isBlank(param.getName())
        ) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        CrmSupplierCreateCloudParam supplierCreateCloudParam = new CrmSupplierCreateCloudParam();
        supplierCreateCloudParam.setCorpKey(corpKey);
        supplierCreateCloudParam.setSourceOrganizationKey(param.getSourceOrganizationKey());
        supplierCreateCloudParam.setSourceOrganizationName(param.getSourceOrganizationName());
        supplierCreateCloudParam.setSourceUserKey(param.getSourceUserKey());
        supplierCreateCloudParam.setSourceUserName(param.getSourceUserName());
        supplierCreateCloudParam.setCategory(param.getCategory());
        supplierCreateCloudParam.setCompanyName(param.getCompanyName());
        supplierCreateCloudParam.setName(param.getName());
        supplierCreateCloudParam.setPhone(param.getPhone());
        supplierCreateCloudParam.setWechatId(param.getWechatId());
        supplierCreateCloudParam.setPlate(param.getPlate());
        supplierCreateCloudParam.setPlateKey(param.getPlateKey());
        supplierCreateCloudParam.setCollaborativeProjectsKey(param.getCollaborativeProjectsKey());
        supplierCreateCloudParam.setCollaborativeProjects(param.getCollaborativeProjects());
        supplierCreateCloudParam.setCooperationModeDesc(param.getCooperationModeDesc());
        supplierCreateCloudParam.setCommissionSharingDesc(param.getCommissionSharingDesc());
        supplierCreateCloudParam.setRemark(param.getRemark());
        supplierCreateCloudParam.setAnnexKey(param.getAnnexKey());
        supplierCreateCloudParam.setRemark(param.getRemark());
        supplierCreateCloudParam.setCreateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        supplierCreateCloudParam.setCreateUserName(YoukeyiSystemContext.getUserName());
        return crmSupplierCloudService.create(supplierCreateCloudParam);
    }

    @Override
    public ResponseContent update(BusinessSupplierUpdateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param
                || StringUtils.isBlank(param.getSupplierPrimaryKey())
        ) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        CrmSupplierUpdateCloudParam supplierUpdateCloudParam = new CrmSupplierUpdateCloudParam();
        supplierUpdateCloudParam.setSupplierPrimaryKey(param.getSupplierPrimaryKey());
        supplierUpdateCloudParam.setSourceOrganizationKey(param.getSourceOrganizationKey());
        supplierUpdateCloudParam.setSourceOrganizationName(param.getSourceOrganizationName());
        supplierUpdateCloudParam.setSourceUserKey(param.getSourceUserKey());
        supplierUpdateCloudParam.setSourceUserName(param.getSourceUserName());
        supplierUpdateCloudParam.setCategory(param.getCategory());
        supplierUpdateCloudParam.setCompanyName(param.getCompanyName());
        supplierUpdateCloudParam.setName(param.getName());
        supplierUpdateCloudParam.setPhone(param.getPhone());
        supplierUpdateCloudParam.setWechatId(param.getWechatId());
        supplierUpdateCloudParam.setPlate(param.getPlate());
        supplierUpdateCloudParam.setPlateKey(param.getPlateKey());
        supplierUpdateCloudParam.setCollaborativeProjectsKey(param.getCollaborativeProjectsKey());
        supplierUpdateCloudParam.setCollaborativeProjects(param.getCollaborativeProjects());
        supplierUpdateCloudParam.setCooperationModeDesc(param.getCooperationModeDesc());
        supplierUpdateCloudParam.setCommissionSharingDesc(param.getCommissionSharingDesc());
        supplierUpdateCloudParam.setRemark(param.getRemark());
        supplierUpdateCloudParam.setAnnexKey(param.getAnnexKey());
        supplierUpdateCloudParam.setRemark(param.getRemark());
        supplierUpdateCloudParam.setUpdateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        supplierUpdateCloudParam.setUpdateUserName(YoukeyiSystemContext.getUserName());
        return crmSupplierCloudService.update(supplierUpdateCloudParam);
    }

    @Override
    public ResponseContent delete(BusinessSupplierDeleteCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param || CollectionUtils.isEmpty(param.getSupplierPrimaryKeys())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        CrmSupplierDeleteCloudParam supplierDeleteCloudParam = new CrmSupplierDeleteCloudParam();
        supplierDeleteCloudParam.setSupplierPrimaryKeys(param.getSupplierPrimaryKeys());
        supplierDeleteCloudParam.setUpdateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        supplierDeleteCloudParam.setUpdateUserName(YoukeyiSystemContext.getUserName());
        return crmSupplierCloudService.delete(supplierDeleteCloudParam);
    }
}
